package com.lcx.movie.controller;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lcx.movie.common.Rep;
import com.lcx.movie.entity.ViewHistory;
import com.lcx.movie.service.ViewHistoryService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
/**
 * @author 李丽君
 * @version 1.0
 * @Description 浏览记录控制层
 * @date 2025/3/3 16:38
 */
@RestController
@CrossOrigin(origins = "*")
public class ViewHistoryController {
    @Autowired
    private ViewHistoryService viewHistoryService;
    /**
     * 前台新增浏览记录
     * @param viewHistory 浏览记录实体
     * @return 受影响的行数
     */
    @PostMapping("/insertViewHistory")
    public String insertViewHistory(@RequestBody ViewHistory viewHistory){
        int insertViewHistory = viewHistoryService.insertViewHistory(viewHistory);
        if (insertViewHistory > 0){
            return "新增浏览记录成功";
        }else {
            return "新增浏览记录失败";
        }
    }
    /**
     * 根据用户id查询浏览记录
     * @param userId 用户id
     * @return 浏览记录集合
     */
    @GetMapping("/selectViewHistory")
    public Rep selectViewHistory(@Param("userId") Integer userId){
        List<ViewHistory> viewHistories = viewHistoryService.selectViewHistory(userId);
        return new Rep().addData("viewHistories", viewHistories);
    }

    /**
     * 根据电影id移除浏览记录
     * @param viewId 浏览记录id
     * @return 受影响的行数
     */
    @GetMapping("/deleteViewHistoryByViewId")
    public String deleteViewHistoryByViewId(@Param("viewId") Integer viewId){
        int deleteViewHistoryByMovieId = viewHistoryService.deleteViewHistoryByViewId(viewId);
        if (deleteViewHistoryByMovieId > 0){
            return "移除浏览记录成功";
        }else{
            return "移除浏览记录失败";
        }
    }
    /**
     * 后台管理员查询浏览记录
     * @return 浏览记录集合
     */
    @GetMapping("/manageSelectViewHistory")
    public Rep manageSelectViewHistory(@Param("page") Integer page,@Param("pageSize") Integer pageSize,
            @Param("movieCnName") String movieCnName){
        //开启分页
        PageHelper.startPage(page,pageSize);
        // 执行查询
        List<ViewHistory> viewHistoryList = this.viewHistoryService.manageSelectViewHistory(movieCnName);
        PageInfo<ViewHistory> viewHistoryPageInfo=new PageInfo<>(viewHistoryList);
        return new Rep().addData("viewHistoryPageInfo", viewHistoryPageInfo);
    }
}
